﻿namespace TuanHA_Combat_Routine
{
    using Styx.Common;
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Net;
    using System.Reflection;
    using System.Web;
    using System.Windows.Forms;

    public class THLogin : Form
    {
        private CheckBox AutoLogin;
        private IContainer components;
        private Label label1;
        private Label label2;
        private Label label3;
        private LinkLabel linkLabel1;
        private Button LoginButton;
        private TextBox PasswordtextBox;
        private CheckBox RememberMe;
        private CheckBox ShowPassword;
        private TextBox UsernametextBox;

        public THLogin()
        {
            this.InitializeComponent();
        }

        private void AutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            THLoginSettings.Instance.AutoLogin = this.AutoLogin.Checked;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.label1 = new Label();
            this.UsernametextBox = new TextBox();
            this.PasswordtextBox = new TextBox();
            this.label2 = new Label();
            this.LoginButton = new Button();
            this.RememberMe = new CheckBox();
            this.ShowPassword = new CheckBox();
            this.linkLabel1 = new LinkLabel();
            this.label3 = new Label();
            this.AutoLogin = new CheckBox();
            base.SuspendLayout();
            this.label1.AutoSize = true;
            this.label1.Location = new Point(12, 15);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x37, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Username";
            this.UsernametextBox.Location = new Point(0x49, 12);
            this.UsernametextBox.Name = "UsernametextBox";
            this.UsernametextBox.Size = new Size(180, 20);
            this.UsernametextBox.TabIndex = 1;
            this.UsernametextBox.Enter += new EventHandler(this.UsernametextBoxSelectAll);
            this.UsernametextBox.KeyDown += new KeyEventHandler(this.PressEnter_KeyDown);
            this.PasswordtextBox.Location = new Point(0x49, 0x26);
            this.PasswordtextBox.Name = "PasswordtextBox";
            this.PasswordtextBox.Size = new Size(180, 20);
            this.PasswordtextBox.TabIndex = 3;
            this.PasswordtextBox.Enter += new EventHandler(this.PasswordtextBoxSelectAll);
            this.PasswordtextBox.KeyDown += new KeyEventHandler(this.PressEnter_KeyDown);
            this.label2.AutoSize = true;
            this.label2.Location = new Point(12, 0x29);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x35, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Password";
            this.LoginButton.Location = new Point(0x49, 0x40);
            this.LoginButton.Name = "LoginButton";
            this.LoginButton.Size = new Size(0x4b, 0x17);
            this.LoginButton.TabIndex = 4;
            this.LoginButton.Text = "Login";
            this.LoginButton.UseVisualStyleBackColor = true;
            this.LoginButton.Click += new EventHandler(this.LoginButton_Click);
            this.RememberMe.AutoSize = true;
            this.RememberMe.Location = new Point(0x49, 0x5d);
            this.RememberMe.Name = "RememberMe";
            this.RememberMe.Size = new Size(0x5f, 0x11);
            this.RememberMe.TabIndex = 5;
            this.RememberMe.Text = "Remember Me";
            this.RememberMe.UseVisualStyleBackColor = true;
            this.RememberMe.CheckedChanged += new EventHandler(this.RememberMe_CheckedChanged);
            this.ShowPassword.AutoSize = true;
            this.ShowPassword.Location = new Point(0x49, 0x74);
            this.ShowPassword.Name = "ShowPassword";
            this.ShowPassword.Size = new Size(0x63, 0x11);
            this.ShowPassword.TabIndex = 6;
            this.ShowPassword.Text = "ShowPassword";
            this.ShowPassword.UseVisualStyleBackColor = true;
            this.ShowPassword.CheckedChanged += new EventHandler(this.ShowPassword_CheckedChanged);
            this.linkLabel1.AutoSize = true;
            this.linkLabel1.Location = new Point(70, 0x88);
            this.linkLabel1.Name = "linkLabel1";
            this.linkLabel1.Size = new Size(0x5d, 13);
            this.linkLabel1.TabIndex = 7;
            this.linkLabel1.TabStop = true;
            this.linkLabel1.Text = "Forgot Login Info?";
            this.linkLabel1.LinkClicked += new LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
            this.label3.AutoSize = true;
            this.label3.Location = new Point(12, 0xa1);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0xe8, 13);
            this.label3.TabIndex = 8;
            this.label3.Text = "WARNING: Do Not Share Account Information!";
            this.AutoLogin.AutoSize = true;
            this.AutoLogin.Location = new Point(0x9a, 0x44);
            this.AutoLogin.Name = "AutoLogin";
            this.AutoLogin.Size = new Size(0x4d, 0x11);
            this.AutoLogin.TabIndex = 9;
            this.AutoLogin.Text = "Auto Login";
            this.AutoLogin.UseVisualStyleBackColor = true;
            this.AutoLogin.CheckedChanged += new EventHandler(this.AutoLogin_CheckedChanged);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x109, 0xb6);
            base.Controls.Add(this.AutoLogin);
            base.Controls.Add(this.label3);
            base.Controls.Add(this.linkLabel1);
            base.Controls.Add(this.ShowPassword);
            base.Controls.Add(this.RememberMe);
            base.Controls.Add(this.LoginButton);
            base.Controls.Add(this.PasswordtextBox);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.UsernametextBox);
            base.Controls.Add(this.label1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "THLogin";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Login";
            base.TopMost = true;
            base.Load += new EventHandler(this.THLogin_Load);
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://www.tuanha.biz/index.php/component/bookpro/?view=login");
        }

        private void LoginButton_Click(object sender, EventArgs e)
        {
            string str = this.UsernametextBox.Text.Trim();
            string str2 = this.PasswordtextBox.Text.Trim();
            if ((str.Length < 1) || (str2.Length < 1))
            {
                MessageBox.Show("Invalid Username or Password.", "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                if (THLoginSettings.Instance.AutoLogin)
                {
                    THLoginSettings.Instance.AutoLogin = false;
                }
            }
            else
            {
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(string.Format("http://www.tuanha.biz/index.php?option=com_bookpro&controller=customer&task=bpajaxlogin&username={0}&password={1}&msp={2}&md5={3}&char={4}&realm={5}&version={6}", new object[] { str, str2, "s", Classname.CreateMD5Hash("Please" + str2 + "don'tshack" + str + "me"), Classname.SafeCharName, Classname.RealName, Classname.VersionBuild }));
                string str4 = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
                THLoginSettings.Instance.Username = str;
                THLoginSettings.Instance.Password = str2;
                if (str4 == "true")
                {
                    Classname.FailLoginAttempt = 0;
                    Classname.LoginCheckVal = true;
                    Styx.Common.Logging.Write("----------------------------------");
                    Styx.Common.Logging.Write("Thank you for supporting TuanHA Combat Routine.");
                    Styx.Common.Logging.Write("----------------------------------");
                    Styx.Common.Logging.Write("Please be advised that account sharing may result in access to the Combat Routine being blocked.");
                    Styx.Common.Logging.Write("----------------------------------");
                    if (!this.RememberMe.Checked)
                    {
                        THLoginSettings.Instance.Username = "";
                        THLoginSettings.Instance.Password = "";
                    }
                    THLoginSettings.Instance.Save();
                    base.Close();
                }
                else
                {
                    Classname.FailLoginAttempt++;
                    Classname.LoginCheckVal = false;
                    Styx.Common.Logging.Write("Invalid Username or Password. ({0})", new object[] { Classname.FailLoginAttempt });
                    Styx.Common.Logging.Write("Make sure use www.tuanha.biz login info NOT svn login info.");
                    if (Classname.FailLoginAttempt > 5)
                    {
                        Classname.FailLoginNextTime = DateTime.Now + TimeSpan.FromSeconds((double) ((Classname.FailLoginAttempt - 10) * 2));
                        MessageBox.Show("Invalid Username or Password. (" + Classname.FailLoginAttempt + ")\nMake sure use www.tuanha.biz login info NOT svn login info.\nClick Forgot Login Info? to reset your password.", "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        MessageBox.Show("Invalid Username or Password. \nMake sure use www.tuanha.biz login info NOT svn login info.", "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
        }

        private void MyTimer_Tick(object sender, EventArgs e)
        {
            if (Classname.FailLoginNextTime <= DateTime.Now)
            {
                if (!this.LoginButton.Enabled)
                {
                    this.LoginButton.Enabled = true;
                }
            }
            else if (this.LoginButton.Enabled)
            {
                this.LoginButton.Enabled = false;
            }
        }

        private void PasswordtextBoxSelectAll(object sender, EventArgs e)
        {
            this.PasswordtextBox.SelectAll();
        }

        private void PressEnter_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Enter) && (Classname.FailLoginNextTime <= DateTime.Now))
            {
                this.LoginButton_Click(null, null);
            }
        }

        private void RememberMe_CheckedChanged(object sender, EventArgs e)
        {
            THLoginSettings.Instance.RememberMe = this.RememberMe.Checked;
            if (this.RememberMe.Checked)
            {
                this.AutoLogin.Enabled = true;
            }
            else
            {
                this.AutoLogin.Enabled = false;
            }
        }

        private void ShowPassword_CheckedChanged(object sender, EventArgs e)
        {
            THLoginSettings.Instance.ShowPassword = this.ShowPassword.Checked;
            if (this.ShowPassword.Checked)
            {
                this.PasswordtextBox.UseSystemPasswordChar = false;
            }
            else
            {
                this.PasswordtextBox.UseSystemPasswordChar = true;
            }
        }

        private void THLogin_Load(object sender, EventArgs e)
        {
            Classname.SafeCharName = "dnt";
            Classname.SafeCharName = HttpUtility.UrlEncode(Classname.SafeCharName);
            Classname.RealName = "dnt";
            Classname.RealName = HttpUtility.UrlEncode(Classname.RealName);
            if (System.IO.File.Exists(Utilities.AssemblyDirectory + @"\Routines\TuanHAShamanSpecialEdition\TuanHA_Combat_Routine.dll"))
            {
                Classname.VersionBuild = Assembly.LoadFrom(Utilities.AssemblyDirectory + @"\Routines\TuanHAShamanSpecialEdition\TuanHA_Combat_Routine.dll").GetName().Version.ToString();
            }
            Classname.VersionBuild = HttpUtility.UrlEncode(Classname.VersionBuild);
            this.AutoLogin.Checked = THLoginSettings.Instance.AutoLogin;
            this.RememberMe.Checked = THLoginSettings.Instance.RememberMe;
            this.ShowPassword.Checked = THLoginSettings.Instance.ShowPassword;
            if (THLoginSettings.Instance.RememberMe)
            {
                this.UsernametextBox.Text = THLoginSettings.Instance.Username;
                this.PasswordtextBox.Text = THLoginSettings.Instance.Password;
                this.AutoLogin.Enabled = true;
            }
            else
            {
                this.AutoLogin.Enabled = false;
            }
            if (THLoginSettings.Instance.ShowPassword)
            {
                this.PasswordtextBox.UseSystemPasswordChar = false;
            }
            else
            {
                this.PasswordtextBox.UseSystemPasswordChar = true;
            }
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer {
                Interval = 0x3e8
            };
            timer.Tick += new EventHandler(this.MyTimer_Tick);
            timer.Start();
            if ((this.AutoLogin.Checked && (this.UsernametextBox.Text.Length > 0)) && (this.PasswordtextBox.Text.Length > 0))
            {
                this.LoginButton_Click(null, null);
            }
        }

        private void UsernametextBoxSelectAll(object sender, EventArgs e)
        {
            this.UsernametextBox.SelectAll();
        }
    }
}

